package com.satan.juc.demo6;

public class FinalReferenceDemo {
    final int[] arrays;
    private FinalReferenceDemo finalReferenceDemo;

    public FinalReferenceDemo() {
        arrays = new int[1];  //1
        arrays[0] = 1;        //2
    }

    public void writerOne() {
        finalReferenceDemo = new FinalReferenceDemo(); //3
    }

    public void writerTwo() {
        arrays[0] = 2;  //4
    }

    public void reader() {
        if (finalReferenceDemo != null) {  //5
            int temp = finalReferenceDemo.arrays[0];  //6
            System.out.println("temp = " + temp);
        }
    }

    public static void main(String[] args) {
        FinalReferenceDemo finalReferenceDemo = new FinalReferenceDemo();
        Thread thread1 = new Thread(()->{
            finalReferenceDemo.writerOne();
        });
        Thread thread2 = new Thread(()->{
            finalReferenceDemo.writerTwo();
        });
        Thread thread3 = new Thread(()->{
            finalReferenceDemo.reader();
        });

        thread1.start();
        thread2.start();
        thread3.start();
    }
}